package com.example.usb.dao.util;

import android.database.Cursor;
import android.text.TextUtils;

import com.example.usb.MyApplication;
import com.example.usb.ai.AiV3Bean;
import com.lib.uvccamera.bean.FaceBean;
import com.example.usb.dao.DaoManager;
import com.example.usb.dao.DbConstant;
import com.example.usb.dao.entity.FaceInfoBean;
import com.example.usb.greendao.FaceInfoBeanDao;
import com.lib.uvccamera.utils.DebugLog;

import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;


/**
 * Created Date 2019/2/21
 *
 * @author liangkun
 * description：人脸数据库操作的工具类
 */
public class FaceDaoUtil {
    private static final String TAG = "FaceDaoUtil";
    private static FaceDaoUtil instance;

    /**
     * 单例模式
     *
     * @return 单例
     */
    public static FaceDaoUtil getInstance() {
        if (instance == null) {
            synchronized (FaceDaoUtil.class) {
                if (instance == null) {
                    instance = new FaceDaoUtil();
                }
            }
        }
        return instance;
    }

    /**
     * 获取图片dao
     *
     * @return dao管理器
     */
    public FaceInfoBeanDao getFaceInfoDao() {
        return DaoManager.getInstance(MyApplication.getContext()).getDaoSession().getFaceInfoBeanDao();
    }

    /**
     * 根据firgue查询到人头路径
     *
     * @param figure 人头uid
     * @return 人头路径
     */
    public String getHeadPathByFigure(String figure) {

        WhereCondition whereCondition = FaceInfoBeanDao.Properties.Figure.eq(figure);
        List<FaceInfoBean> list = getFaceInfoDao().queryBuilder().where(whereCondition).list();
        if (list != null && list.size() > 0) {
            FaceInfoBean infoBean = list.get(0);
            return infoBean.getFileHeadPath();
        }
        return null;
    }

    /**
     * 根据firgue查询到数据
     *
     * @param figure 人头uid
     * @return 人头bean
     */
    public FaceInfoBean getFaceBeanByFinger(String figure) {
        WhereCondition whereCondition = FaceInfoBeanDao.Properties.Figure.eq(figure);
        List<FaceInfoBean> list = getFaceInfoDao().queryBuilder().where(whereCondition).list();
        if (list != null && list.size() > 0) {
            FaceInfoBean faceInfoBean = list.get(0);
            DebugLog.d(TAG + " getFigerScore有数据-----------figure---------" + figure);
            return faceInfoBean;
        } else {
            DebugLog.d(TAG + " getFigerScore 无数据-----------figure---------" + figure);
        }
        return null;
    }


    /**
     * 根据firgue查询到照片数据
     *
     * @param figure 人头figure
     * @return 人头bean
     */
    public int getPicCountByFigure(String figure) {
        WhereCondition whereCondition = FaceInfoBeanDao.Properties.Figure.eq(figure);
        List<FaceInfoBean> list = getFaceInfoDao().queryBuilder().where(whereCondition).list();
        if (null != list && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    /**
     * @param figure    人脸标识
     * @param headpath  人头
     * @param faceScore 人脸分数
     */
    public void updateOldFigureData(String figure, String headpath, double faceScore) {

        WhereCondition whereCondition = FaceInfoBeanDao.Properties.Figure.eq(figure);
        List<FaceInfoBean> list = getFaceInfoDao().queryBuilder().where(whereCondition).list();
        List<FaceInfoBean> tempList = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                FaceInfoBean faceInfoBean = list.get(i);
                faceInfoBean.setFileHeadPath(headpath);
                faceInfoBean.setFaceScore(faceScore);
//                if (ChaneHeadUtil.ERROR_SCORE != faceScore) {
//                    faceInfoBean.setFaceScore(faceScore);
//                }
                tempList.add(faceInfoBean);
            }
            //多条修改
            getFaceInfoDao().updateInTx(tempList);
        }

    }




    /**
     * 通过finger分组查询
     *
     * @return 分组后的需要合并的人脸figner
     */
    public List<String> getFaceString() {
        //分组查询语句
        Cursor cursor = DaoManager.getReadableDatabase(MyApplication.getContext()).rawQuery(DbConstant.SQL_FINGER_GROUP, null);
        List<String> beans = new ArrayList<>();
        while (cursor.moveToNext()) {
            String finger = cursor.getString(cursor.getColumnIndex(FaceInfoBeanDao.Properties.Figure.columnName));
              if(!TextUtils.isEmpty(finger)){
                  beans.add(finger);
              }

            }
        cursor.close();
        return beans;
    }





    /**
     * 更新人头数据,把二更新到一
     *
     * @param finger    最优
     * @param fingerTwo 需要更新
     * @param headPath  最优人头路径
     */
    public void updateFaceByFinger(String finger, String fingerTwo, String headPath) {
        //查询到keyFingerTwo的所有本地数据库
        QueryBuilder<FaceInfoBean> builder = getFaceInfoDao().queryBuilder();
        WhereCondition whereCondition = FaceInfoBeanDao.Properties.Figure.eq(fingerTwo);
        List<FaceInfoBean> list = builder.where(whereCondition).list();
        List<FaceInfoBean> tempList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            FaceInfoBean infoBean = list.get(i);
            infoBean.setFigure(finger);
            infoBean.setFileHeadPath(headPath);
            tempList.add(infoBean);
        }
        //全部以一起更新
        getFaceInfoDao().updateInTx(tempList);
    }

    /**
     * 插入人脸属性数据
     *
     * @param faceList  人脸信息
     * @param picMd5    图片md5
     * @param faceScore 人脸分数
     * @param finger    人脸唯一标志
     */
    public void insertFaceBean(AiV3Bean.FaceList faceList, String picMd5, double faceScore, String finger, String headPath, String secondHeadPath, String photoPath) {
        FaceInfoBean infoBean = new FaceInfoBean();
        //设置主键
        infoBean.setUuid(UUID.randomUUID().toString());
        infoBean.setFaceMd5(picMd5);
        infoBean.setFigure(finger);
        infoBean.setFaceScore(faceScore);
        infoBean.setFileHeadPath(headPath);
        infoBean.setSecondHeadPath(secondHeadPath);
        infoBean.setPhotoCompressPath(photoPath);
        /**
         * 人脸信息
         */
        infoBean.setBlur(faceList.getQuality().getBlur());
        infoBean.setIllumination(faceList.getQuality().getIllumination());
        infoBean.setCompleteness(faceList.getQuality().getCompleteness());

        infoBean.setLeftEye(faceList.getQuality().getOcclusion().getLeftEye());
        infoBean.setRightEye(faceList.getQuality().getOcclusion().getRightEye());
        infoBean.setNose(faceList.getQuality().getOcclusion().getNose());
        infoBean.setMouth(faceList.getQuality().getOcclusion().getMouth());
        infoBean.setLefCheek(faceList.getQuality().getOcclusion().getLeftCheek());
        infoBean.setRightCheek(faceList.getQuality().getOcclusion().getRightCheek());
        infoBean.setChinContour(faceList.getQuality().getOcclusion().getChinContour());

        infoBean.setFaceType(faceList.getFaceType().getType());
        infoBean.setFaceProbability(faceList.getFaceType().getProbability());

        infoBean.setLocationLeft(faceList.getLocation().getLeft());
        infoBean.setLocationTop(faceList.getLocation().getTop());
        infoBean.setLocationWidth(faceList.getLocation().getWidth());
        infoBean.setLocationHeight(faceList.getLocation().getHeight());
        infoBean.setLocationRotation(faceList.getLocation().getRotation());

        infoBean.setFaceProbability(faceList.getFaceProbability());
        infoBean.setAge(faceList.getAge());
        infoBean.setBeauty(faceList.getBeauty());
        infoBean.setYaw(faceList.getAngle().getYaw());
        infoBean.setPitch(faceList.getAngle().getPitch());
        infoBean.setRoll(faceList.getAngle().getRoll());

        infoBean.setSmlieType(faceList.getExpression().getType());
        infoBean.setSmlieProbability(faceList.getExpression().getProbability());

        infoBean.setGenderType(faceList.getGender().getType());
        infoBean.setGenderProbability(faceList.getGender().getProbability());

        infoBean.setGlassType(faceList.getGlasses().getType());
        infoBean.setGlassProbability(faceList.getGlasses().getProbability());
        //插入数据库
        getFaceInfoDao().insertOrReplace(infoBean);

    }
}
